﻿/*
	wp8libflac project
	© Alovchin, 2014
*/

using System;
using System.Windows;
using System.Windows.Threading;
using Windows.Storage;
using Microsoft.Phone.BackgroundAudio;

namespace wp8.App
{
    public partial class MainPage
    {
        // Constructor
        public MainPage()
        {
            this.InitializeComponent();

            var zeroTimeSpan = TimeSpan.FromSeconds(0.0);
            this.TimeText.Text = String.Format("{0:0}:{1:00}", zeroTimeSpan.Minutes, zeroTimeSpan.Seconds);
        }

        private async void OnButtonClick(object sender, RoutedEventArgs e)
        {
#error Add your test file to the 'Audio' folder of this project.

            StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(
                new Uri("ms-appx:///Audio/yourfile.flac", UriKind.Absolute));

            BackgroundAudioPlayer.Instance.Volume = 1.0;
            BackgroundAudioPlayer.Instance.Track = new AudioTrack(
                null,
                "Goodbye, Cruel World",
                "Pink Floyd",
                "The Wall",
                null,
                file.Path,
                EnabledPlayerControls.All);

            var timer = new DispatcherTimer {Interval = TimeSpan.FromSeconds(0.5)};
            timer.Tick += (o, args) =>
            {
                var currentTime = BackgroundAudioPlayer.Instance.Position;
                this.TimeText.Text = String.Format("{0:0}:{1:00}", currentTime.Minutes, currentTime.Seconds);
            };
            timer.Start();
        }
    }
}